package com.lanou.zy.projecta.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.lanou.zy.projecta.R;
import com.lanou.zy.projecta.util.OnItemClickListener;
import com.lanou.zy.projecta.volley.VolleySingleton;
import com.lanou.zy.projecta.entity.World;

import java.util.List;

/**
 * Created by dllo on 15/10/27.
 */
public class HotCountryRecyclerViewAdapter extends RecyclerView.Adapter<HotCountryRecyclerViewAdapter.HotCountryViewHolder> {
    private ImageLoader imageLoader;
    private List<World.DataEntity.HotCountryEntity> hotCountryEntities;
    private LayoutInflater inflater;
    private OnItemClickListener onItemClickListener;
    private Context context;

    public HotCountryRecyclerViewAdapter(Context c) {
        this.context = c;
        imageLoader = VolleySingleton.getInstance().getImageLoader();
    }

    public void addData(List<World.DataEntity.HotCountryEntity> datas) {
        hotCountryEntities = datas;
        notifyDataSetChanged();
    }

    @Override
    public HotCountryViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
        inflater = LayoutInflater.from(viewGroup.getContext());
        View v = inflater.inflate(R.layout.destination_recycler_view_item, null);
        return new HotCountryViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final HotCountryViewHolder holder, int position) {
        if (hotCountryEntities != null && hotCountryEntities.size() > 0) {
            World.DataEntity.HotCountryEntity wdh = hotCountryEntities.get(position);

            holder.CountryCNNTv.setText(wdh.getCnname());
            holder.CountryENNTv.setText(wdh.getEnname());
            holder.CityNameTv.setText(wdh.getLabel());
            holder.CityCountTv.setText(wdh.getCount() + "");

            holder.countryImgIv.setImageUrl(wdh.getPhoto(), imageLoader);

            holder.countryLinearlayout.setBackgroundColor(getimageColor(holder.countryImgIv));
        }

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getAdapterPosition();
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            });
        }
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.onItemClickListener = itemClickListener;
    }

    @Override
    public int getItemCount() {
        return hotCountryEntities != null && hotCountryEntities.size() > 0 ? hotCountryEntities.size() : 0;
    }

    public class HotCountryViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView countryImgIv;
        private TextView CityCountTv, CityNameTv, CountryCNNTv, CountryENNTv;
        private LinearLayout countryLinearlayout;

        public HotCountryViewHolder(View v) {
            super(v);
            countryImgIv = (NetworkImageView) v.findViewById(R.id.countryImgIv);
            CityCountTv = (TextView) v.findViewById(R.id.CityCountTv);
            CityNameTv = (TextView) v.findViewById(R.id.CityNameTv);
            CountryCNNTv = (TextView) v.findViewById(R.id.CountryCNNTv);
            CountryENNTv = (TextView) v.findViewById(R.id.CountryENNTv);
            countryLinearlayout = (LinearLayout) v.findViewById(R.id.countryLinearlayout);
        }

    }

    private int getimageColor(ImageView imageView) {

        int c = 0;
        if (imageView.getDrawable() != null) {
            Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
            //获取一张位图
            if (bitmap != null) {

                //获取一个Builder
                Palette.Builder from = Palette.from(bitmap);
                //生成颜色 Builder.generate().getter方法 这里简单演示
                c = from.generate().getDarkVibrantColor(context.getResources().getColor(android.R.color.black));
            }
        }
        return c;
    }
}
